//
//  DealImagesViewController.m
//  ACSProduct
//
//  Created by preet dhillon on 04/08/12.
//  Copyright (c) 2012 dhillon. All rights reserved.
//

#import "DealImagesViewController.h"
#import "Global.h"
#define MINIMUM_SCALE 1.0
#define MAXIMUM_SCALE 6.0
@implementation DealImagesViewController
@synthesize strImgName,imgDeal,arrImg,ScrlviewDeal,pgCtrlDeal,tag,viewZoom,ScrlviewZoom;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     [self.ScrlviewDeal setContentSize:CGSizeMake([self.arrImg count]*320,355)];
    [self.view addSubview:self.ScrlviewDeal];
    float inX=0;
    for (int i=0; i<[arrImg  count]; i++) 
    {
        imagVen = [[VenDealImageView alloc] initWithFrame:CGRectMake(inX, 0, self.ScrlviewDeal.frame.size.width,355)];
        imagVen.client= self;
        imagVen.tag = i;
        
        imagVen.imageVenDeal.placeholderImage = [UIImage imageNamed:@"PlaceHolder.png"];
        [imagVen setviewWithUrl:[NSURL  URLWithString:[arrImg objectAtIndex:i]]];
        
        [self.ScrlviewDeal addSubview:imagVen];
        /*imgDeal=[[EGOImageView alloc]initWithFrame:CGRectMake(inX,0, 320,355)];
        ScrlviewZoom =[[UIScrollView alloc]initWithFrame:CGRectMake(inX,0, 320,355)];
        viewZoom=[[UIView alloc]initWithFrame:CGRectMake(inX,0, 320,355)];
        [viewZoom setBackgroundColor:[UIColor orangeColor]];
        
        [ScrlviewZoom setDelegate:self];
        ScrlviewZoom.maximumZoomScale=2.5;
        ScrlviewZoom.minimumZoomScale=1.0;
        imgDeal.placeholderImage    = [UIImage imageNamed:@"PlaceHolder.png"];
        imgDeal.imageURL=[NSURL URLWithString:[arrImg objectAtIndex:i]];
        pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
        pinchGesture.scale = 1;
        [viewZoom setMultipleTouchEnabled:YES];
        [viewZoom setUserInteractionEnabled:YES];
        [imgDeal setMultipleTouchEnabled:YES];
        [imgDeal setUserInteractionEnabled:YES];
        [pinchGesture setDelegate:self];

        //[imgDeal addGestureRecognizer:pinchGesture];
        [viewZoom addSubview:imgDeal];
        [ScrlviewZoom addSubview:viewZoom];
        [self.ScrlviewDeal addSubview:ScrlviewZoom]; */
        inX += 320;
       // incX += (self.webOfferView.frame.origin.x+self.webOfferView.frame.size.width);
    }
    
      self.pgCtrlDeal.currentPage = tag;
    self.pgCtrlDeal.numberOfPages =[arrImg count];
    
    [self.ScrlviewDeal setContentOffset:CGPointMake(320*tag, 0) animated:NO];

    UIButton *btnBack=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setFrame:CGRectMake(0, 10, 50, 30)];
    [btnBack setImage:[UIImage imageNamed:@"Back.png"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(clickToBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:btnBack];

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
    return YES;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)clickToBackBtn:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
    static CGRect initialBounds;
    
    UIView *_view = sender.view;
    
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        initialBounds = _view.bounds;
    }
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    
    CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
    _view.bounds = CGRectApplyAffineTransform(initialBounds, zt);
   
    return;
}
- (void)pinch:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded
        || gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"gesture.scale = %f", gesture.scale);
        UIView *_viewPinch = gesture.view;
        CGFloat currentScale = _viewPinch.frame.size.width / _viewPinch.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;
        
        if (newScale < MINIMUM_SCALE) {
            newScale = MINIMUM_SCALE;
        }
        if (newScale > MAXIMUM_SCALE) {
            newScale = MAXIMUM_SCALE;
        }
        
        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        _viewPinch.transform = transform;
        gesture.scale = 1;
    }
}

#pragma mark- UIScrollView Delegates

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)ScrollView {
    if([ScrollView isEqual:ScrlviewZoom])
        return viewZoom;
    return nil;
}
#pragma mark- UIScrollview Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
    CGFloat pageWidth = scrollView.frame.size.width;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pgCtrlDeal.currentPage = page; 
}
-(void)doubleTouch:(NSInteger)tag
{
}
@end
